import React from 'react';
import { useState } from 'react';
import { open } from '@tauri-apps/api/dialog';
import { convertToMarkdown } from './utils/converter';

function App() {
  const [files, setFiles] = useState<string[]>([]);
  const [outputDir, setOutputDir] = useState<string>('');
  const [converting, setConverting] = useState(false);

  const selectFiles = async () => {
    const selected = await open({
      multiple: true,
      filters: [{
        name: 'Word文档',
        extensions: ['doc', 'docx']
      }]
    });
    
    if (Array.isArray(selected)) {
      setFiles(selected);
    }
  };

  const selectOutputDir = async () => {
    const selected = await open({
      directory: true,
    });
    
    if (typeof selected === 'string') {
      setOutputDir(selected);
    }
  };

  const handleConvert = async () => {
    if (!files.length || !outputDir) {
      alert('请选择文件和输出目录');
      return;
    }

    setConverting(true);
    try {
      await convertToMarkdown(files, outputDir);
      alert('转换完成！');
    } catch (error) {
      alert(`转换失败: ${error}`);
    }
    setConverting(false);
  };

  return (
    <div className="container">
      <h1>Word转Markdown工具</h1>
      
      <div className="controls">
        <button onClick={selectFiles}>
          选择Word文件
        </button>
        <div className="files-list">
          已选择 {files.length} 个文件
        </div>

        <button onClick={selectOutputDir}>
          选择输出目录
        </button>
        <div className="output-dir">
          输出目录: {outputDir || '未选择'}
        </div>

        <button 
          onClick={handleConvert}
          disabled={converting || !files.length || !outputDir}
        >
          {converting ? '转换中...' : '开始转换'}
        </button>
      </div>
    </div>
  );
}

export default App; 